<template>
	<view>
		<view class="head">
			<view class="head-bar">
				<uni-search-bar @confirm="search" :focus="true" v-model="searchValue" @input="input" @cancel="cancel"
					@clear="clear">
				</uni-search-bar>
			</view>
			<view class="head-btn" @click="search">搜索</view>
		</view>
		<view class="cont">
			<view class="item" v-for="(item,index) in list" :key="index"
				@click="toPage('/sub-community/post/post?id='+item.post_id)">
				<view class="item-top">
					<image :src="item.userInfo.avatarUrl" class="top-img"></image>
					<view class="">
						<view class="top-name">{{item.userInfo.nickName}}</view>
						<view class="top-time">{{item.time | formatDate}}</view>
					</view>
				</view>
				<view class="item-bom">
					<view class="item-title">{{item.title}}</view>
					<view class="item-text">{{item.details}}</view>
					<view class="img-box">
						<view class="item-img" v-for="(item1,index1) in item.imgList.slice(0,3)" :key="index1">
							<image :src="item1" class="img"></image>
						</view>
					</view>
					<view class="icon-box">
						<view class="icon-item">
							<image src="/static/images/icon/icon-zan.png" class="icon"></image>
						</view>
						<view class="icon-item">
							<image src="/static/images/icon/icon-ping.png" class="icon"></image>
						</view>
						<view class="icon-item">
							<image src="/static/images/icon/icon-share.png" class="icon"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="icon-add" @click="toPage('/sub-community/release/release')">
			<image src="../../static/images/icon/icon-release.png" class="img"></image>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				isLogin: uni.getStorageSync('isLogin'),
				list: [],
				page: 1,
				searchValue: '',
			}
		},
		computed: {},
		watch: {},
		methods: {
			toPage(e) {
				let isLogin = uni.getStorageSync('isLogin')
				if (!isLogin) {
					uni.showToast({
						icon: 'none',
						title: '未登录'
					})
					return
				}
				uni.navigateTo({
					url: e
				})
			},
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				const db = wx.cloud.database()
				
				let list0 = this.list
				let skip = 8
				db.collection('post').orderBy('time', 'desc').skip(skip * (this.page - 1)).limit(skip).get().then(res => {
					let list = res.data
					let list1 = list0.concat(list)
					this.list = list1
					uni.hideLoading()
				})
				
			},
			search(res) {
				// console.log(res)
				const db = wx.cloud.database()
				const _ = db.command
				db.collection('post').where(_.or([{
						title: db.RegExp({
							regexp: this.searchValue,
							options: 'i',
						}),
					},
					{
						details: db.RegExp({
							regexp: this.searchValue,
							options: 'i',
						}),
					}
				])).get().then(res => {
					console.log(res.data)
					this.list = res.data
					// if (res.data.length != 0) {
					// 	console.log(res)
					// } else {
					// 	wx.showToast({
					// 		title: '未找到',
					// 		icon: 'none'
					// 	})
					// }
				})

			},
			input(res) {
				console.log(res)
			},
			clear(res) {
				console.log(res)
			},
			cancel(res) {
				console.log(res)
				this.page = 1
				this.list = []
				this.getList()
			}
		},
		onLoad(options) {
			this.getList()
		},
		onShow() {
			this.list = [],
				this.page = 1,
				this.getList()
		},
		onReady() {},
		onPullDownRefresh() {},
		onReachBottom() {
			// console.log(111)
			this.page += 1
			this.getList()
		},
		onShareAppMessage() {},
		onPageScroll() {},

	}
</script>

<style lang="scss" scoped>
	.head {
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.head-bar {
			width: 580rpx;
			// border: 1rpx solid red;
		}

		.head-btn {
			font-size: 28rpx;
			width: 120rpx;
			height: 60rpx;
			text-align: center;
			line-height: 60rpx;
			background: linear-gradient(#00AEEC, #1296db);
			color: #fff;
			border-radius: 30rpx;
			margin-right: 30rpx;
		}
	}

	.cont {
		margin-top: 30rpx;

		.item {
			padding-top: 20rpx;
			padding-bottom: 20rpx;
			width: 690rpx;
			margin-left: 30rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			border-radius: 10rpx;

			.item-top {
				display: flex;
				align-items: center;
				margin-left: 20rpx;

				.top-img {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					margin-right: 20rpx;
				}

				.top-name {
					font-size: 28rpx;
				}

				.top-time {
					font-size: 24rpx;
					color: #bfbfbf;
				}
			}

			.item-bom {
				margin-top: 20rpx;
				margin-left: 20rpx;
				margin-right: 20rpx;

				.item-title {
					font-size: 30rpx;
				}

				.item-text {
					font-size: 26rpx;
					margin-top: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.img-box {
					display: grid;
					justify-content: space-between;
					grid-template-columns: repeat(auto-fill, 180rpx);
					margin-top: 30rpx;

					.img {
						width: 180rpx;
						height: 180rpx;
					}
				}

				.icon-box {
					margin-top: 30rpx;
					display: flex;
					justify-content: space-between;

					.icon {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}
	}

	.icon-add {
		position: fixed;
		right: 50rpx;
		bottom: 100rpx;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		background: linear-gradient(#00AEEC, #1296db);

		.img {
			width: 40rpx;
			height: 40rpx;
		}
	}
</style>
